#!/usr/bin/env node

//类
const Koa = require('koa');

const app = new Koa();

app.use(async (ctx,next)=>{ //M-logger
  next();
  const cost = ctx.cost;

  //const cost = await next();
  //const cost = ctx.response.header.cost;
  console.log(`${ctx.method} ${ctx.path} ${cost}ms`);

    
});


app.use((ctx,next)=>{ //M-ms
  const start = Date.now();
  next();
  const end = Date.now();
  //console.log(`time:${end-start}ms`);
  //ctx.header.cost = end-start;
  //ctx.set('cost',end-start);
  //return end-start;
  ctx.cost = end-start;
});

app.use((ctx,next)=>{ //M-hello中间件
  ctx.body = 'hello world!';
  
});

app.listen(8080);

console.log('hello koa2');
